<template>
  <view class="container">
    <view class="title">API请求示例</view>
    
    <view class="section">
      <view class="section-title">用户登录</view>
      <view class="form">
        <input v-model="loginForm.username" type="text" placeholder="用户名" />
        <input v-model="loginForm.password" type="password" placeholder="密码" />
        <button type="primary" @click="handleLogin">登录</button>
      </view>
    </view>
    
    <view class="section">
      <view class="section-title">获取商品列表</view>
      <button type="primary" @click="handleGetGoodsList">获取商品列表</button>
      <view class="goods-list" v-if="goodsList.length > 0">
        <view class="goods-item" v-for="(item, index) in goodsList" :key="index">
          <view class="goods-name">{{ item.name }}</view>
          <view class="goods-price">¥ {{ item.price }}</view>
        </view>
      </view>
      <view class="empty" v-else>暂无商品数据</view>
    </view>
    
    <view class="section">
      <view class="section-title">上传图片</view>
      <button type="primary" @click="handleUploadImage">选择并上传图片</button>
      <image v-if="imageUrl" :src="imageUrl" mode="aspectFit" class="preview-image"></image>
    </view>
    
    <view class="section">
      <view class="section-title">结果展示</view>
      <scroll-view scroll-y class="result-box">
        <text>{{ resultText }}</text>
      </scroll-view>
    </view>
  </view>
</template>

<script>
import { userService, goodsService, uploadService } from '@/utils/api';

export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      goodsList: [],
      imageUrl: '',
      resultText: ''
    };
  },
  
  methods: {
    // 处理登录
    async handleLogin() {
      if (!this.loginForm.username || !this.loginForm.password) {
        uni.showToast({
          title: '请输入用户名和密码',
          icon: 'none'
        });
        return;
      }
      
      try {
        this.showLoading('登录中...');
        const res = await userService.login(this.loginForm);
        this.hideLoading();
        
        // 保存登录信息
        uni.setStorageSync('token', res.data.token);
        
        // 展示结果
        this.resultText = JSON.stringify(res, null, 2);
        
        uni.showToast({
          title: '登录成功',
          icon: 'success'
        });
      } catch (error) {
        this.hideLoading();
        this.resultText = error.message || '登录失败';
      }
    },
    
    // 获取商品列表
    async handleGetGoodsList() {
      try {
        this.showLoading('加载中...');
        const res = await goodsService.getGoodsList({
          page: 1,
          size: 10
        });
        this.hideLoading();
        
        this.goodsList = res.data.list || [];
        this.resultText = JSON.stringify(res, null, 2);
      } catch (error) {
        this.hideLoading();
        this.resultText = error.message || '获取商品列表失败';
      }
    },
    
    // 上传图片
    handleUploadImage() {
      uni.chooseImage({
        count: 1,
        success: (chooseImageRes) => {
          const tempFilePaths = chooseImageRes.tempFilePaths;
          this.uploadImage(tempFilePaths[0]);
        }
      });
    },
    
    // 上传图片到服务器
    async uploadImage(filePath) {
      try {
        this.showLoading('上传中...');
        const res = await uploadService.uploadImage(filePath);
        this.hideLoading();
        
        this.imageUrl = res.data.url || '';
        this.resultText = JSON.stringify(res, null, 2);
        
        uni.showToast({
          title: '上传成功',
          icon: 'success'
        });
      } catch (error) {
        this.hideLoading();
        this.resultText = error.message || '上传图片失败';
      }
    },
    
    // 显示加载提示
    showLoading(title = '加载中') {
      uni.showLoading({
        title,
        mask: true
      });
    },
    
    // 隐藏加载提示
    hideLoading() {
      uni.hideLoading();
    }
  }
};
</script>

<style lang="scss">
.container {
  padding: 30rpx;
  
  .title {
    font-size: 36rpx;
    font-weight: bold;
    text-align: center;
    margin-bottom: 40rpx;
  }
  
  .section {
    margin-bottom: 40rpx;
    background-color: #fff;
    border-radius: 12rpx;
    padding: 20rpx;
    box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.1);
    
    .section-title {
      font-size: 30rpx;
      font-weight: bold;
      margin-bottom: 20rpx;
      border-bottom: 1px solid #eee;
      padding-bottom: 10rpx;
    }
    
    .form {
      display: flex;
      flex-direction: column;
      
      input {
        height: 80rpx;
        border: 1px solid #eee;
        border-radius: 8rpx;
        padding: 0 20rpx;
        margin-bottom: 20rpx;
      }
      
      button {
        margin-top: 20rpx;
      }
    }
    
    .goods-list {
      margin-top: 20rpx;
      
      .goods-item {
        display: flex;
        justify-content: space-between;
        padding: 20rpx 0;
        border-bottom: 1px solid #f5f5f5;
        
        .goods-name {
          flex: 1;
          font-size: 28rpx;
        }
        
        .goods-price {
          color: #ff6700;
          font-weight: bold;
          font-size: 28rpx;
        }
      }
    }
    
    .empty {
      text-align: center;
      color: #999;
      padding: 30rpx 0;
    }
    
    .preview-image {
      width: 100%;
      height: 300rpx;
      margin-top: 20rpx;
    }
    
    .result-box {
      height: 300rpx;
      border: 1px solid #eee;
      border-radius: 8rpx;
      padding: 20rpx;
      margin-top: 20rpx;
      font-size: 24rpx;
      color: #333;
      word-break: break-all;
      line-height: 1.5;
      background-color: #f9f9f9;
    }
  }
}
</style> 